package com.bjsxt.livegoods.login.service.service.impl;

import com.bjsxt.livegoods.dao.user.UserDao;
import com.bjsxt.livegoods.entity.User;
import com.bjsxt.livegoods.login.service.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserDao userDao;
    @Override
    public Map<String, Object> login(String username, String password) {
        Map<String,Object> result=new HashMap<>();
        User user=userDao.findUserByPhone(username);
        if(user==null){
            //用户不存在
            result.put("status",500);
            result.put("msg","登录失败");
            return result;
        }
        //判断验证码（密码），需要考虑验证码有效时长
        Date currentTime=new Date();
        Date expire=user.getExpire();
        if((currentTime.getTime()-expire.getTime())<(5*60*1000)){
            //验证码有效
           if(password.equals(user.getYzm())){
               result.put("status",200);
               result.put("msg","登陆成功");
               return result;
           }else{
               //验证码错误
               result.put("status","500");
               result.put("msg","登录失败");
               return result;
           }
        }else{
            //验证码无效
            result.put("status","500");
            result.put("msg","登录失败");
            return result;
        }

    }
}
